/*  
* Copyright (c) 2010, James Daniello
* This project is licensed under the 'New BSD' license.
* The full text of the license can be found in the root
* directory under the filename "LICENSE"
*/
package src.engine;

import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsConfiguration;


/**
* This class combines and utilizes many methods available to Java2D to create optimized BufferedImages. Most resources were
* found or derived from resources available to the public on the Internet. The only public method is createNewOptimizedImage()
* which takes a BufferedImage and returns a BufferedImage
*/
public class PICAIUJava2DImageOptimizer{

  /*
  * These methods are borrowed from my other project, Moldering. They have been shown to increase efficiency by creating
  * optimized graphics for machine running the code.
  */
  
  /**
  * Create a BufferedImage optimized for the current machine's graphics card.
  * @param image the BufferedImage to be optimized
  * @return an optimized BufferedImage
  */
  public static BufferedImage createNewOptimizedImage(BufferedImage image) {
      BufferedImage temp = createCompatibleDestImage(image, image.getWidth(), image.getHeight());
      temp = toCompatibleImage(image);
      return temp;
  }
  
  private static BufferedImage createCompatibleDestImage(BufferedImage image, int width, int height) {
      return isHeadless()
              ? new BufferedImage(width, height, image.getType())
              : getGraphicsConfiguration2().createCompatibleImage(width, height,
              image.getTransparency());
  }
  private static GraphicsConfiguration getGraphicsConfiguration2() {
      return GraphicsEnvironment.getLocalGraphicsEnvironment().
              getDefaultScreenDevice().getDefaultConfiguration();
  }
  private static boolean isHeadless() {
      return GraphicsEnvironment.isHeadless();
  }
  
  /**
  * Creates an image compatible with the current machine
  */
  private static BufferedImage toCompatibleImage(BufferedImage image) {
    /*
     * This method's Source taken from:
     * GraphicsUtilities.java,v 1.14 2009/03/10 14:43:53 kschaefe Exp $
     *
     * Dual-licensed under LGPL (Sun and Romain Guy) and BSD (Romain Guy).
     *
     * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
     * Santa Clara, California 95054, U.S.A. All rights reserved.
     *
     * Copyright (c) 2006 Romain Guy <romain.guy@mac.com>
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     * 3. The name of the author may not be used to endorse or promote products
     *    derived from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    if (GraphicsEnvironment.isHeadless()) {
        return image;
    }

    if (image.getColorModel().equals(
            GraphicsEnvironment.getLocalGraphicsEnvironment().
            getDefaultScreenDevice().getDefaultConfiguration().getColorModel())) {
        return image;
    }

    BufferedImage compatibleImage =
            GraphicsEnvironment.getLocalGraphicsEnvironment().
            getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(
            image.getWidth(), image.getHeight(),
            image.getTransparency());
    Graphics g = compatibleImage.getGraphics();

    try {
        g.drawImage(image, 0, 0, null);
    } finally {
        g.dispose();
    }

    return compatibleImage;
  }

}